﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data.SqlClient;
using System.Data;

namespace DataAccess.Operate
{
    public class DA_Project_Point_Inherit:SqlInteractionBase
    {
        public DataEntity.T_Project_Point SelectOne(string id)
        {
            string sSql = "select * from t_Project_Point where Project_Point_MainID='"+id+"'";
            SqlCommand cmdToExecute = new SqlCommand();
            SqlDataAdapter adapter = new SqlDataAdapter(cmdToExecute);
            DataTable toReturn = new DataTable("t_Project_Point");
            DataEntity.T_Project_Point Project_Point = new DataEntity.T_Project_Point(); 

            cmdToExecute.CommandText = sSql;
            cmdToExecute.CommandType = CommandType.Text;

            try
            {
                cmdToExecute.Connection = _mainConnection;
                if (_mainConnectionIsCreatedLocal)
                {
                    _mainConnection.Open();
                }
                else
                {
                    if (_mainConnectionProvider.IsTransactionPending)
                    {
                        cmdToExecute.Transaction = _mainConnectionProvider.CurrentTransaction;
                    }
                }

                adapter.Fill(toReturn);
                if (toReturn.Rows.Count > 0)
                {
                    Project_Point.Project_Point_ID = (Guid)toReturn.Rows[0]["Project_Point_ID"];
                    Project_Point.Project_Point_MainID = (Guid)toReturn.Rows[0]["Project_Point_MainID"];
                    Project_Point.Project_Point_FirstA1 = (string)toReturn.Rows[0]["Project_Point_FirstA1"]; 
                    Project_Point.Project_Point_SecondA1 = (Int32)toReturn.Rows[0]["Project_Point_SecondA1"];
                    Project_Point.Project_Point_SecondA2 = (string)toReturn.Rows[0]["Project_Point_SecondA2"];
                    Project_Point.Project_Point_ThirdA1 = (string)toReturn.Rows[0]["Project_Point_ThirdA1"];
                    Project_Point.Project_Point_ThirdA2 = (string)toReturn.Rows[0]["Project_Point_ThirdA2"]; 
                }
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message.ToString());
            }
            finally
            {
                if (_mainConnectionIsCreatedLocal)
                {
                    _mainConnection.Close();
                }

                cmdToExecute.Dispose();

                adapter.Dispose();
            }

            return Project_Point;
        }

        public bool DeleteByMainID(string id)
        {
            string sSql = "delete from t_Project_Point where Project_Point_MainID='" + id + "'";
            SqlCommand cmdToExecute = new SqlCommand();

            cmdToExecute.CommandText = sSql;
            cmdToExecute.CommandType = CommandType.Text;

            try
            {
                cmdToExecute.Connection = _mainConnection;
                if (_mainConnectionIsCreatedLocal)
                {
                    _mainConnection.Open();
                }
                else
                {
                    if (_mainConnectionProvider.IsTransactionPending)
                    {
                        cmdToExecute.Transaction = _mainConnectionProvider.CurrentTransaction;
                    }
                }

                _rowsAffected = cmdToExecute.ExecuteNonQuery();
                return true;

            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message.ToString());
            }
            finally
            {
                if (_mainConnectionIsCreatedLocal)
                {
                    _mainConnection.Close();
                }

                cmdToExecute.Dispose();

  
            }

        }
    }
}
